export function getHistory() {
    return wx.getStorageSync('history') || { house: [], dynamic: [], strategy: [] }
}

export function addHouseHistory(data) {
    const app = getApp()
    const list = app.history.house || []
    const index = list.findIndex(item => item.house_id === data.house_id)
    if (index > -1) {
        list.splice(index, 1)
    } else if (list.length >= 50) {
        list.length = 49
    }
    list.unshift(data)
    app.history.house = list

    saveHistory(app.history)
}


export function addStrategyHistory(data) {
    const app = getApp()
    const list = app.history.strategy || []
    const index = list.findIndex(item => item.strategy_id === data.strategy_id)
    if (index > -1) {
        list.splice(index, 1)
    } else if (list.length >= 50) {
        list.length = 49
    }
    list.unshift(data)
    app.history.strategy = list

    saveHistory(app.history)
}

export function addDynamicHistory(data) {
    const app = getApp()
    const list = app.history.dynamic || []
    const index = list.findIndex(item => item.tourise_id === data.tourise_id)
    if (index > -1) {
        list.splice(index, 1)
    } else if (list.length >= 50) {
        list.length = 49
    }
    list.unshift(data)
    app.history.dynamic = list

    saveHistory(app.history)
}

export function saveHistory(history) {
    wx.setStorageSync('history', history)
}
